13 point(int X
, int Y
) : x(X
), y(Y
) {}
16 point
pivot(10000, 10000);
18 ostream
& operator<< (ostream
& out
, const point
& c
)
20 out
<< "(" << c
.x
<< "," << c
.y
<< ")";
24 double area(const vector
<point
> &p
){
26 for (int i
=0; i
<p
.size(); ++i
){
27 int j
= (i
+1) % p
.size();
28 r
+= p
[i
].x
*p
[j
].y
- p
[j
].x
*p
[i
].y
;
33 //retorna si c esta a la izquierda de el segmento AB
34 int cross(const point
&a
, const point
&b
, const point
&c
){
35 return (b
.x
-a
.x
)*(c
.y
-a
.y
) - (c
.x
-a
.x
)*(b
.y
-a
.y
);
38 bool angleCmp(const point
&self
, const point
&that
){
39 return( cross(pivot
, that
, self
) < 0 );
42 int distsqr(point a
, point b
){
43 return (a
.x
- b
.x
)*(a
.x
- b
.x
) + (a
.y
- b
.y
)*(a
.y
- b
.y
);
46 //vector p tiene los puntos ordenados anticlockwise
47 vector
<point
> graham(vector
<point
> p
){
49 sort(p
.begin(), p
.end(), angleCmp
);
50 for (int i
=1; i
<p
.size()-1; ++i
){
51 if (cross(p
[0], p
[i
], p
[i
+1]) == 0){
52 if (distsqr(p
[0], p
[i
]) < distsqr(p
[0], p
[i
+1])){
53 p
.erase(p
.begin() + i
);
55 p
.erase(p
.begin() + i
+ 1);
61 /*cout << "Candidatos para el Convex Hull: " << endl;
62 for (int i=0; i<p.size(); ++i) cout << p[i] << " ";
65 vector
<point
> chull(p
.begin(), p
.begin()+3);
68 for (int i
=3; i
<p
.size(); ++i
){
69 while ( chull
.size() >= 2 && cross(chull
[chull
.size()-2], chull
[chull
.size()-1], p
[i
]) < 0){
70 chull
.erase(chull
.end() - 1);
72 chull
.push_back(p
[i
]);
75 /* cout << "Nuestro Chull es: " << endl;
76 for (int i=0; i<chull.size(); ++i) cout << chull[i] << " ";
85 while (cin
>> n
&& n
){
87 point
min(10000, 10000);
89 for (int i
=0; i
<n
; ++i
){
92 p
.push_back(point(x
,y
));
93 if (y
< min
.y
|| (y
== min
.y
&& x
< min
.x
)){
99 /*cout << cross(p[0], p[1], p[2]) << endl;
100 cout << "El punto " << p[2] << " esta a la " << (cross(p[0], p[1], p[2]) < 0 ? "derecha":"izquierda") << " del segmento " << p[0] << p[1] << endl;*/
103 double tileArea
= area(p
);
104 vector
<point
> sortedP
;
107 for (int j
=minPos
,cuenta
=1; cuenta
<=p
.size(); ++cuenta
, j
= (j
+1)%p
.size()){
108 sortedP
.push_back(p
[j
]);
111 for (int j
=minPos
, cuenta
=1; cuenta
<=p
.size(); ++cuenta
, j
= (j
-1 < 0?j
-1+p
.size():j
-1)){
112 sortedP
.push_back(p
[j
]);
115 //cout << "Area es: " << area(p) << endl;
116 /*for (int i=0; i<sortedP.size(); ++i) cout << sortedP[i] << " ";
118 tileArea
= fabs(tileArea
);
119 double chullArea
= fabs(area(graham(sortedP
)));
121 cout
<< "Tile #"<<tileNo
++<<endl
;
122 printf("Wasted Space = %.2f \%\n\n", (chullArea
- tileArea
) * 100.0 / chullArea
);